const { delay } = require("../../utils/util");
var init;
// pages/qa/qa.js
let app =  getApp();
import("./../../utils/util")
//背景音乐的地址
let musicUrl = "http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/audio/bgm.mp3";
let back =wx.getBackgroundAudioManager();//背景音乐控制器
Page({

    /**
     * Page initial data
     */
    data: {
        name: '',//校区名称
        index: 1, //进度，默认是第一题
        qa: [],//问题和答案
        // questionImg: 'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/qa/lighton.png',//灯泡图片地址，默认是亮着的
        background_choice_a: '#5aa37f',
        background_choice_b: '#5aa37f',
        background_choice_c: '#5aa37f',
        background_choice_d: '#5aa37f',
        ispass: 0,

        //计时器相关的数据
        hour:0,
        minute:0,
        second:0,
        millisecond:0,
        timecount:'00:00:00',
        cost:0,
        flag:1,
        endtime:"",
        time:0,
        startTime:0,
        cut:0,
    },

    backmusic(){
        player();
        function player(){
          back.title = "答题";
          back.src = musicUrl;
          back.onEnded(() => {
            player();
          })
        }
    },

    //获得成就
    getAchivements(changeName) {
        wx.request({
            url: app.globalData.urlHome + '/ach/set',
            data: {
                "openid": wx.getStorageSync('openid'),
                "changeName": changeName,
            },
            header: {'content-type':'application/json'},
            method: 'POST',
            success: (result) => {
                console.log(result);
            },
            fail: () => {},
            complete: () => {}
        });
    },

    //计时器相关的逻辑
    start:function(){
        clearInterval(init);
        var that=this;
        that.setData({
          hour:0,
          minute:0,
          second:0,
          millisecond:0
        })
        init=setInterval(function(){
            that.timer()
        },50);
    },

    stop:function(){
        clearInterval(init);
    },

    Reset:function(){
        var that=this;
        clearInterval(init);
        that.setData({
          hour:0,
          minute:0,
          second:0,
          millisecond:0,
          timecount:'00:00:00'
        })
    },

    timer:function(){
        var that = this;
        // console.log(that.data.millisecond)
        that.setData({
          millisecond:that.data.millisecond+5
        })
        if(that.data.millisecond>=100){
          that.setData({
            millisecond:0,
            second:that.data.second + 1
          })
        }
        that.setData({
          timecount:that.data.hour+":"+that.data.minute+":"+that.data.second+":"+that.data.millisecond
        })
    },

    //上一题
    before(e) {
        var that = this;
        for(var i = 0;i < 5;i ++) {
            that.setData({
                ['qa[' + i + '].background_choice_a']: '#5aa37f',
                ['qa[' + i + '].background_choice_b']: '#5aa37f',
                ['qa[' + i + '].background_choice_c']: '#5aa37f',
                ['qa[' + i + '].background_choice_d']: '#5aa37f',
            })
        }

        if(that.data.index !== 1) {//不是第一题
            that.setData({
                index: that.data.index - 1,
                cut: that.data.second,
            })
        }else {
            wx.showModal({
                title: '提示',
                content: '已经是第一题了',
                showCancel: false,
                cancelText: '取消',
                cancelColor: '#000000',
                confirmText: '确定',
                confirmColor: '#3CC51F',
                success: (result) => {
                    if (result.confirm) {
                        
                    }
                },
                fail: () => {},
                complete: () => {}
            });
              
        }
    },  

    //下一题
    next(e) {
        var that = this;
        for(var i = 0;i < 5;i ++) {
            that.setData({
                ['qa[' + i + '].background_choice_a']: '#5aa37f',
                ['qa[' + i + '].background_choice_b']: '#5aa37f',
                ['qa[' + i + '].background_choice_c']: '#5aa37f',
                ['qa[' + i + '].background_choice_d']: '#5aa37f',
            })
        }

        if(that.data.index !== 5) { //不是最后一题
            that.setData({
                index: that.data.index + 1,
                cut: that.data.second,
            })
        }else { //是最后一题
            var yes = true;//是否完成该校区所有题目
            for(var i = 0;i < that.data.qa.length;i ++) {// 判断是否完成该校区所有题目
                if(that.data.qa[i].hasanswered !== 1) { //有题目未作答成功
                    yes = false;
                    break;
                }
            }

            if(yes === false) { //未完成所有题目
                wx.showModal({
                    title: '提示',
                    content: '已经是最后一题了',
                    showCancel: false,
                    cancelText: '取消',
                    cancelColor: '#000000',
                    confirmText: '确定',
                    confirmColor: '#3CC51F',
                });
            }else {//完成所有题目
                wx.showModal({
                    title: '提示',
                    content: '恭喜您完成该校区全部题目！点击“确定”返回主页面。',
                    showCancel: false,
                    cancelText: '取消',
                    cancelColor: '#000000',
                    confirmText: '确定',
                    confirmColor: '#3CC51F',
                    success: (result) => {
                        if (result.confirm) {
                            wx.request({ //更新状态
                                url: app.globalData.urlHome + '/state/set',
                                data: {
                                    "openid": wx.getStorageSync('openid'),
                                    "changeName": that.data.name,
                                },
                                header: {'content-type':'application/json'},
                                method: 'POST',
                                success: (result) => {
                                    wx.navigateBack({
                                        delta: 1
                                    });
                                },
                                fail: () => {},
                                complete: () => {}
                            });
                        }
                    },
                    fail: () => {},
                    complete: () => {}
                });
            }
        }
    },

    //选择题目
    choose(e) {
        var that = this;
        var audio = wx.createInnerAudioContext();
                audio.src="http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/audio/wrong.mp3";
        if(that.data.qa[that.data.index - 1].hasanswered === 0) { //未完成作答
            //设定作答题目数，用来计算正确率
            if(that.data.qa[that.data.index - 1].isOne === true) { //是第一次作答
                var count = wx.getStorageSync('count');
                if(count === '') {
                    wx.setStorageSync('count', 1);
                }else {
                    count ++;
                    wx.setStorageSync('count', count);
                }

            }
            
            if(e.currentTarget.dataset.name === that.data.qa[that.data.index - 1].answer) {//回答正确
                //音效
                let audio = wx.createInnerAudioContext();
                audio.src = "http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/audio/correct.mp3"
                audio.play();

                //设定作答正确题目数，用于计算正确率
                if(that.data.qa[that.data.index - 1].isOne === true) { //是第一次作答
                    var CorrectCount = wx.getStorageSync('correctCount');
                    if(CorrectCount === '') {
                        wx.setStorageSync('correctCount', 1);
                    }else {
                        CorrectCount ++;
                        wx.setStorageSync('correctCount', CorrectCount);
                    }

                    //设置连续回答正确的题目数
                    if(wx.getStorageSync('concorrect') === '') {
                        wx.setStorageSync('concorrect', 1);
                    }else {
                        wx.setStorageSync('concorrect', wx.getStorageSync('concorrect') + 1);
                    }

                    //清空连续错误
                    wx.setStorageSync('conwrong', 0);
                      
                    //获取“晨光熹微”成就
                    if(wx.getStorageSync('concorrect') === 5) {
                        that.getAchivements("morning");
                    }

                    //获取“光风霁月”成就
                    if(wx.getStorageSync('concorrect') === 10) {
                        that.getAchivements("wind");
                    }

                    //获取“电光火石”成就
                    if(that.data.second - that.data.cut <= 10) {
                        that.getAchivements("thunder");
                    }
                }

                //设定第一次作答为false
                that.setData({
                    ['qa[' + (that.data.index - 1) + '].isOne']: false,
                })

                that.setData({
                    ['qa[' + (that.data.index - 1) + '].questionImg']: 'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/qa/lightoff.png', //灯泡灭
                    ['qa[' + (that.data.index - 1) + '].hasanswered']: 1,//设置该题目已作答成功
                    ['qa[' + (that.data.index - 1) + '].switch_' + that.data.qa[that.data.index - 1].answer]: 'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/qa/switchoff.png',
                    ['qa[' + (that.data.index - 1) + '].width'] : '1000rpx',
                    ['qa[' + (that.data.index - 1) + '].y']: '0rpx',
                })
                //睡眠1000毫秒
                setTimeout(() => {
                    if(that.data.index !== 5) { //不是最后一题
                        var yes = true;//是否完成该校区所有题目
                        for(var i = 0;i < that.data.qa.length;i ++) {// 判断是否完成该校区所有题目
                            if(that.data.qa[i].hasanswered !== 1) { //有题目未作答成功
                                yes = false;
                                break;
                            }
                        }
        
                        if(yes === true) { //已完成所有题目
                            wx.showModal({
                                title: '提示',
                                content: '恭喜您完成该校区全部题目！点击“确定”返回主页面。',
                                showCancel: false,
                                cancelText: '取消',
                                cancelColor: '#000000',
                                confirmText: '确定',
                                confirmColor: '#3CC51F',
                                success: (result) => {
                                    if (result.confirm) {
                                          
    
                                        wx.request({ //更新状态
                                            url: app.globalData.urlHome + '/state/set',
                                            data: {
                                                "openid": wx.getStorageSync('openid'),
                                                "changeName": that.data.name,
                                            },
                                            header: {'content-type':'application/json'},
                                            method: 'POST',
                                            success: (result) => {
                                                // 获取一下现在的状态
                                                wx.request({
                                                    url: app.globalData.urlHome + '/state/one',
                                                    data: {
                                                        "openid": wx.getStorageSync('openid'),                                                      
                                                    },
                                                    header: {'content-type':'application/json'},
                                                    method: 'POST',
                                                    success: (result) => {
                                                        var result_num = result.data;
                                                        var isLast = false;
                                                        // 判断是否是通关的最后一个校区
                                                        if(
                                                            result_num[0].center === 1 &&
                                                            result_num[0].hongjialou === 1 &&
                                                            result_num[0].xinglongshan === 1 &&
                                                            result_num[0].qianfoshan === 1 &&
                                                            result_num[0].baotuquan === 1 &&
                                                            result_num[0].ruanjianyuan === 1 &&
                                                            result_num[0].qingdao === 1 &&
                                                            result_num[0].weihai === 1
                                                        ) {
                                                            isLast = true;
                                                        }
    
                                                        if(isLast === true) {
                                                            
                                                            wx.showModal({
                                                                title: '恭喜',
                                                                content: '恭喜！您已经通关所有校区，请查看您的报告！',
                                                                showCancel: false,
                                                                cancelText: '取消',
                                                                cancelColor: '#000000',
                                                                confirmText: '查看',
                                                                confirmColor: '#3CC51F',
                                                                success: (result) => {
                                                                    if (result.confirm) {
                                                                        //更新报告数据
                                                                        wx.request({
                                                                            url: app.globalData.urlHome + '/rep/upd',
                                                                            data: {
                                                                                "openid": wx.getStorageSync('openid'),
                                                                                "changeName": "time",
                                                                                "changeNum": wx.getStorageSync('time') + that.data.second,
                                                                            },
                                                                            header: {'content-type':'application/json'},
                                                                            method: 'POST',
                                                                            success: (result) => {
                                                                                console.log("Update success");
                                                                            },
                                                                            fail: () => {},
                                                                            complete: () => {}
                                                                        });
                                                                        
                                                                        wx.request({
                                                                            url: app.globalData.urlHome + '/rep/upd',
                                                                            data: {
                                                                                "openid": wx.getStorageSync('openid'),
                                                                                "changeName": "number",
                                                                                "changeNum": 8,
                                                                            },
                                                                            header: {'content-type':'application/json'},
                                                                            method: 'POST',
                                                                            success: (result) => {
                                                                                console.log("Update success");
                                                                            },
                                                                            fail: () => {},
                                                                            complete: () => {}
                                                                        });
    
                                                                        wx.navigateTo({
                                                                            url: './../report/report',
                                                                        });
                                                                    }
                                                                },
                                                                fail: () => {},
                                                                complete: () => {}
                                                            });
                                                              
                                                        }else { //不是通关的最后一个校区
                                                            wx.navigateBack({ //返回主页面
                                                                delta: 1
                                                            });
                                                        }
                                                    },
                                                    fail: () => {},
                                                    complete: () => {}
                                                });
                                            },
                                            fail: () => {},
                                            complete: () => {}
                                        });
                                    }
                                },
                                fail: () => {},
                                complete: () => {}
                            });
                        }else {
                            that.setData({
                                index: that.data.index + 1,//自动进入下一题
                                cut: that.data.second,
                            })
                        }
        
                    }else { //最后一题
                        var yes = true;//是否完成该校区所有题目
                        for(var i = 0;i < that.data.qa.length;i ++) {// 判断是否完成该校区所有题目
                            if(that.data.qa[i].hasanswered !== 1) { //有题目未作答成功
                                yes = false;
                                break;
                            }
                        }
        
                        if(yes === true) { //完成所有题目
                            //音效
                            var audio = wx.createInnerAudioContext();
                            audio.src="http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/audio/pass.mp3"
                            audio.play();

                            wx.showModal({
                                title: '提示',
                                content: '恭喜您完成该校区全部题目！点击“确定”返回主页面。',
                                showCancel: false,
                                cancelText: '取消',
                                cancelColor: '#000000',
                                confirmText: '确定',
                                confirmColor: '#3CC51F',
                                success: (result) => {
                                    if (result.confirm) {
    
                                        wx.request({ //更新状态
                                            url: app.globalData.urlHome + '/state/set',
                                            data: {
                                                "openid": wx.getStorageSync('openid'),
                                                "changeName": that.data.name,
                                            },
                                            header: {'content-type':'application/json'},
                                            method: 'POST',
                                            success: (result) => {
                                                // 获取一下现在的状态
                                                wx.request({
                                                    url: app.globalData.urlHome + '/state/one',
                                                    data: {
                                                        "openid": wx.getStorageSync('openid'),                                                      
                                                    },
                                                    header: {'content-type':'application/json'},
                                                    method: 'POST',
                                                    success: (result) => {
                                                        var result_num = result.data;
                                                        var isLast = false;
                                                        // 判断是否是通关的最后一个校区
                                                        if(
                                                            result_num[0].center === 1 &&
                                                            result_num[0].hongjialou === 1 &&
                                                            result_num[0].xinglongshan === 1 &&
                                                            result_num[0].qianfoshan === 1 &&
                                                            result_num[0].baotuquan === 1 &&
                                                            result_num[0].ruanjianyuan === 1 &&
                                                            result_num[0].qingdao === 1 &&
                                                            result_num[0].weihai === 1
                                                        ) {
                                                            isLast = true;
                                                        }
    
                                                        if(isLast === true) {
                                                            wx.showModal({
                                                                title: '恭喜',
                                                                content: '恭喜！您已经通关所有校区，请查看您的报告！',
                                                                showCancel: false,
                                                                cancelText: '取消',
                                                                cancelColor: '#000000',
                                                                confirmText: '查看',
                                                                confirmColor: '#3CC51F',
                                                                success: (result) => {
                                                                    if (result.confirm) {
                                                                        //更新报告数据
                                                                        wx.request({
                                                                            url: app.globalData.urlHome + '/rep/upd',
                                                                            data: {
                                                                                "openid": wx.getStorageSync('openid'),
                                                                                "changeName": "time",
                                                                                "changeNum": wx.getStorageSync('time') + that.data.second,
                                                                            },
                                                                            header: {'content-type':'application/json'},
                                                                            method: 'POST',
                                                                            success: (result) => {
                                                                                console.log("Update success");
                                                                            },
                                                                            fail: () => {},
                                                                            complete: () => {}
                                                                        });
                                                                        
                                                                        wx.request({
                                                                            url: app.globalData.urlHome + '/rep/upd',
                                                                            data: {
                                                                                "openid": wx.getStorageSync('openid'),
                                                                                "changeName": "number",
                                                                                "changeNum": 8,
                                                                            },
                                                                            header: {'content-type':'application/json'},
                                                                            method: 'POST',
                                                                            success: (result) => {
                                                                                console.log("Update success");
                                                                            },
                                                                            fail: () => {},
                                                                            complete: () => {}
                                                                        });
    
                                                                        wx.navigateTo({
                                                                            url: './../report/report',
                                                                        });
                                                                    }
                                                                },
                                                                fail: () => {},
                                                                complete: () => {}
                                                            });
                                                              
                                                        }else { //不是通关的最后一个校区
                                                            wx.navigateBack({ //返回主页面
                                                                delta: 1
                                                            });
                                                        }
                                                    },
                                                    fail: () => {},
                                                    complete: () => {}
                                                });
                                            },
                                            fail: () => {},
                                            complete: () => {}
                                        });
                                    }
                                },
                                fail: () => {},
                                complete: () => {}
                            });
                        }else {
                            wx.showModal({
                                title: '提示',
                                content: '已经是最后一题，请返回作答未作答的题目。',
                                showCancel: false,
                                cancelText: '取消',
                                cancelColor: '#000000',
                                confirmText: '确定',
                                confirmColor: '#3CC51F',
                                success: (result) => {
                                    if (result.confirm) {
                                        
                                    }
                                },
                                fail: () => {},
                                complete: () => {}
                            });
                              
                        }
                    }
                }, 1000);
            }else { //回答错误
                //音效
                // properties(Read only)(duration,currentTime,paused,buffered,volume)
                // properties(src,startTime,autoplay,loop,obeyMuteSwitch)
                
                audio.play();//here!!!!

                //清空连续正确
                wx.setStorageSync('concorrect', 0);

                //设置连续错误
                if(wx.getStorageSync('conwrong') === '') {
                    wx.setStorageSync('conwrong', 1);
                }else {
                    wx.setStorageSync('conwrong', wx.getStorageSync('conwrong') + 1);
                }

                //获得“浮光掠影”成就
                if(wx.getStorageSync('conwrong') === 5) {
                    that.getAchivements("careless");
                }
                  
                //设置已经作答
                that.setData({
                    ['qa[' + (that.data.index - 1) + '].isOne']: false,
                })

                wx.showModal({
                    title: '提示',
                    content: '回答错误！点击“确认”键继续作答。',
                    showCancel: false,
                    cancelText: '取消',
                    cancelColor: '#000000',
                    confirmText: '确定',
                    confirmColor: '#3CC51F',
                    
                    fail: () => {},
                    complete: () => {}
                });
            }

        }else {
            //音效
            let audio = wx.createInnerAudioContext();
            audio.src = "http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/audio/click.mp3"
            audio.play();
            wx.showModal({
                title: '提示',
                content: '您已完成这道题目了呢，继续完成未完成的题目吧！',
                showCancel: false,
                cancelText: '取消',
                cancelColor: '#000000',
                confirmText: '确定',
                confirmColor: '#3CC51F',
                success: (result) => {
                    if (result.confirm) {
                        
                    }
                },
                fail: () => {},
                complete: () => {}
            });
              
        }
    },



    /**
     * Lifecycle function--Called when page load
     */
    onLoad: function (options) {
        var that = this;
        if(options.name === 'center') {
            //设置标题
            wx.setNavigationBarTitle({
                title: '中心校区',
            });
        }

        if(options.name === 'hongjialou') {
            //设置标题
            wx.setNavigationBarTitle({
                title: '洪家楼校区',
            });
        }

        if(options.name === 'ruanjianyuan') {
            //设置标题
            wx.setNavigationBarTitle({
                title: '软件园校区',
            });
        }

        if(options.name === 'xinglongshan') {
            //设置标题
            wx.setNavigationBarTitle({
                title: '兴隆山校区',
            });
        }

        if(options.name === 'baotuquan') {
            //设置标题
            wx.setNavigationBarTitle({
                title: '趵突泉校区',
            });
        }

        if(options.name === 'qianfoshan') {
            //设置标题
            wx.setNavigationBarTitle({
                title: '千佛山校区',
            });
        }

        if(options.name === 'qingdao') {
            //设置标题
            wx.setNavigationBarTitle({
                title: '青岛校区',
            });
        }

        if(options.name === 'weihai') {
            //设置标题
            wx.setNavigationBarTitle({
                title: '威海校区',
            });
        }

        //设置校区名称
        that.setData({
            name: options.name,              
        })

        //读取答题进度
        if(wx.getStorageSync(options.name) !== '') {
            that.setData({
                index: wx.getStorageSync(options.name),
            })
        }

        //获取题目
        //判断是否已通关
        wx.request({
            url: app.globalData.urlHome + '/state/one',
            data: {
                "openid": wx.getStorageSync('openid'),                                                      
            },
            header: {'content-type':'application/json'},
            method: 'POST',
            success: (result) => {
                var result_num = result.data;
                if(options.name === "qingdao"){
                    if(result_num[0].qingdao === 1){

                        that.setData({
                            ispass: 1,
                        })
                    }
                }else if(options.name === "weihai"){
                    if(result_num[0].weihai === 1){
                        that.setData({
                            ispass: 1,
                        })
                    }
                }else if(options.name === "center"){
                    if(result_num[0].center === 1){
                        that.setData({
                            ispass: 1,
                        })
                    }
                }else if(options.name === "hongjialou"){
                    if(result_num[0].hongjialou === 1){
                        that.setData({
                            ispass: 1,
                        })
                    }
                }else if(options.name === "xinglongshan"){
                    if(result_num[0].xinglongshan === 1){
                        that.setData({
                            ispass: 1,
                        })
                    }
                }else if(options.name === "qianfoshan"){
                    if(result_num[0].qingdao === 1){
                        that.setData({
                            ispass: 1,
                        })
                    }
                }else if(options.name === "baotuquan"){
                    if(result_num[0].qingdao === 1){
                        that.setData({
                            ispass: 1,
                        })
                    }
                }else if(options.name === "ruanjianyuan"){
                    if(result_num[0].qingdao === 1){
                        that.setData({
                            ispass: 1,
                        })
                    }
                }
        wx.request({
            url: app.globalData.urlHome + '/qa/all',
            data: {
                "under": options.name,
            },
            header: {'content-type':'application/json'},
            method: 'POST',
            success: (result) => {
                that.setData({
                    qa: result.data,
                })
                
                        
                //为qa添加属性(isOne)，代表是否是第一次作答
                //为qa添加属性(questionImg),代表本题的灯泡图片
                        if(that.data.ispass === 0)
                        {
                        for(var i = 0;i < that.data.qa.length; i ++) {
                            that.setData({
                                ['qa[' + i + '].isOne']: true,
                                ['qa[' + i + '].questionImg']: 'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/qa/lighton.png',
                                ['qa[' + i + '].switch_a']: 'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/qa/switch.png',
                                ['qa[' + i + '].switch_b']: 'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/qa/switch.png',
                                ['qa[' + i + '].switch_c']: 'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/qa/switch.png',
                                ['qa[' + i + '].switch_d']: 'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/qa/switch.png',
                                ['qa[' + i + '].width'] : '1000rpx', 
                                ['qa[' + i + '].y']: '0rpx',
                                ['qa[' + i + '].background_choice_a']: '#5aa37f',
                                ['qa[' + i + '].background_choice_b']: '#5aa37f',
                                ['qa[' + i + '].background_choice_c']: '#5aa37f',
                                ['qa[' + i + '].background_choice_d']: '#5aa37f',
                            })
                        }}
                        else{
                            for(var i = 0;i < that.data.qa.length; i ++) {
                                that.setData({
                                    index: 1,
                                    ['qa[' + i + '].isOne']: false,
                                    ['qa[' + i + '].questionImg']: 'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/qa/lighton.png',
                                    ['qa[' + i + '].switch_a']: 'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/qa/switch.png',
                                    ['qa[' + i + '].switch_b']: 'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/qa/switch.png',
                                    ['qa[' + i + '].switch_c']: 'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/qa/switch.png',
                                    ['qa[' + i + '].switch_d']: 'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/qa/switch.png',
                                    ['qa[' + i + '].width'] : '1000rpx', 
                                    ['qa[' + i + '].y']: '0rpx',
                                    ['qa[' + i + '].background_choice_a']: '#5aa37f',
                                    ['qa[' + i + '].background_choice_b']: '#5aa37f',
                                    ['qa[' + i + '].background_choice_c']: '#5aa37f',
                                    ['qa[' + i + '].background_choice_d']: '#5aa37f',
                                })
                            }
                        }
                    }
                })


                //获取答题正确情况
                if(wx.getStorageSync(options.name + 'yes') !== '') {
                    var yyds = wx.getStorageSync(options.name + 'yes');
                    
                    for(var i = 0;i < result.data.length;i ++) {
                        that.setData({
                            ['qa[' + i + '].hasanswered'] : yyds[i].hasanswered,
                            ['qa[' + i + '].questionImg'] : yyds[i].questionImg,
                            ['qa[' + i + '].width'] : yyds[i].width,
                            ['qa[' + i + '].y'] : yyds[i].y,
                        })
                    }

                    for(var i = 0;i < result.data.length;i ++) {
                        if(yyds[i].hasanswered === 1) { //已经作答过
                            that.setData({
                                ['qa[' + i + '].switch_' + yyds[i].answer] : 'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/qa/switchoff.png'
                            })
                        }
                    }
                }
            },
            fail: () => {},
            complete: () => {}
        });

          
        
    },

    /**
     * Lifecycle function--Called when page is initially rendered
     */
    onReady: function () {
        
    },

    /**
     * Lifecycle function--Called when page show
     */
    onShow: function () {
        var that = this;
        //启动计时器
        that.start();
        that.backmusic();
    },

    /**
     * Lifecycle function--Called when page hide
     */
    onHide: function () {
        back.pause();
    },

    /**
     * Lifecycle function--Called when page unload
     */
    onUnload: function () {
        var that = this;
        //储存答题进度
        wx.setStorageSync(that.data.name, that.data.index);

        //储存答题正确情况
        wx.setStorageSync(that.data.name + 'yes', that.data.qa);

        //停止计时器
        that.stop();
        
        //记录时间
        if(wx.getStorageSync('time') === '' && that.data.ispass === 0) {
            wx.setStorageSync('time', that.data.second);
        }else if(wx.getStorageSync('time') !== '' && that.data.ispass === 0){
            wx.setStorageSync('time', wx.getStorageSync('time') + that.data.second);
        }
    },

    /**
     * Page event handler function--Called when user drop down
     */
    onPullDownRefresh: function () {

    },

    /**
     * Called when page reach bottom
     */
    onReachBottom: function () {

    },

    /**
     * Called when user click on the top right corner to share
     */
    onShareAppMessage: function () {

    }
})